package com.bwie.controller;

import com.bwie.pojo.Brand;
import com.bwie.service.BrandService;
import com.bwie.utils.PageResult;
import com.bwie.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: day0423-pxx
 * @ClassName BrandController
 * @description:
 * @author: 段阳
 * @create: 2024-04-23 19:44
 **/
@RestController
@RequestMapping("item/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    @GetMapping("/page")
    public ResponseEntity<PageResult<Brand>> pageList(
            @RequestParam(value = "key",required = false) String key,
            @RequestParam(value = "page",defaultValue = "1") Long page,
            @RequestParam(value = "rows",defaultValue = "5") Long rows,
            @RequestParam(value = "sortBy",required = false) String sortBy,
            @RequestParam(value = "desc",defaultValue = "false") Boolean desc
    ){
        PageResult pages = brandService.pageList(page,rows,key,sortBy,desc);
        return ResponseEntity.ok(pages);
    }

    @PostMapping
    public R addNewBrand(Brand brand, @RequestParam("cids")List<Long> cids){
        return brandService.addNewBrand(brand,cids);
    }

    @PutMapping
    public R UPBrand(Brand brand, @RequestParam("cids")List<Long> cids){
        return brandService.UPBrand(brand,cids);
    }

    @PostMapping("/delBrand/{id}")
    public R delBrand(@PathVariable(name = "id")Long id){
        return brandService.delBrand(id);
    }

    @GetMapping("/cid/{bid}")
    public R getBrandInfo(@PathVariable(name = "bid")Long bid){
        return brandService.getBrandInfo(bid);
    }

    /**
     * 根据商品分类的id查询商品分类的信息
     * @param id
     * @return
     */
    @RequestMapping("/queryBrandNameByBrandId")
    public ResponseEntity<Brand> queryBrandNameByBrandId(@RequestParam("id") Long id){
        Brand brand = brandService.queryBrandNameByBrandId(id);

        return ResponseEntity.ok(brand);
    }

}